package com.uzai.console.service.faq.privatework.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.faq.faqreplygroup.BatchUpdateFaqReplyGroupDto;
import com.uzai.console.dto.faq.faqreplygroup.FaqReplyGroupDto;
import com.uzai.console.dto.faq.faqreplygroup.FaqReplyGroupIdDto;
import com.uzai.console.dto.faq.faqreplygroup.FaqReplyGroupQuery;
import com.uzai.console.dto.faq.privatework.FaqReplyQuery;
import com.uzai.console.entity.FaqReply;
import com.uzai.console.entity.FaqReplyGroup;
import com.uzai.console.mapper.FaqReplyGroupMapper;
import com.uzai.console.mapper.FaqReplyMapper;
import com.uzai.console.service.faq.privatework.FaqReplyGroupService;
import com.uzai.console.vo.faq.faqreplygroup.FaqReplyGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class FaqReplyGroupServiceImpl implements FaqReplyGroupService {

    @Autowired
    private FaqReplyGroupMapper faqReplyGroupMapper;
    @Autowired
    private FaqReplyMapper faqReplyMapper;

    /**
     * 分页查询智能回复分组分组列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param faqReplyGroupQuery
     * @return null
     */
    public Page<FaqReplyGroupVo> findFaqReplyGroupByPage(FaqReplyGroupQuery faqReplyGroupQuery){
        //新建返回到页面的LIST对象
        List<FaqReplyGroupVo> faqReplyGroupVoList = new ArrayList<>();

        //执行分页查询对象
        Page<FaqReplyGroup> page = new Page<>();
        faqReplyGroupQuery.convert(page);
        List<FaqReplyGroup> faqReplyGroupList = faqReplyGroupMapper.findByList(page, faqReplyGroupQuery);

        if(faqReplyGroupList != null && faqReplyGroupList.size() > 0){
            for (FaqReplyGroup faqReplyGroup : faqReplyGroupList){
                FaqReplyGroupVo faqReplyGroupVo = new FaqReplyGroupVo();
                BeanUtils.copyProperties(faqReplyGroup, faqReplyGroupVo);
                faqReplyGroupVoList.add(faqReplyGroupVo);
            }
        }

        //返回到页面分页对象
        Page<FaqReplyGroupVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(faqReplyGroupVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加智能回复分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param faqReplyGroupDto
     * @return null
     */
    public void addFaqReplyGroup(FaqReplyGroupDto faqReplyGroupDto){
        if(StringUtils.isBlank(faqReplyGroupDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        //验证是否重名
        FaqReplyGroup faqReplyGroup_name = faqReplyGroupMapper.selectByName(faqReplyGroupDto.getMerId(), faqReplyGroupDto.getName());
        if(faqReplyGroup_name != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
        }

        Integer now = DateUtil.getNowTime();
        FaqReplyGroup faqReplyGroup = new FaqReplyGroup();
        faqReplyGroup.setId(IdWorker.getId());
        faqReplyGroup.setMerId(faqReplyGroupDto.getMerId());
        faqReplyGroup.setName(faqReplyGroupDto.getName());
        faqReplyGroup.setCreateTime(now);
        faqReplyGroup.setUpdateTime(now);
        faqReplyGroupMapper.insert(faqReplyGroup);
    }

    /**
     * 查询超级问答回复语分组详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param faqReplyGroupIdDto
     * @return null
     */
    public FaqReplyGroupVo findFaqReplyGroup(FaqReplyGroupIdDto faqReplyGroupIdDto){
        FaqReplyGroupVo faqReplyGroupVo = new FaqReplyGroupVo();

        if(faqReplyGroupIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择分组");
        }

        FaqReplyGroup faqReplyGroup = faqReplyGroupMapper.selectById(faqReplyGroupIdDto.getId(), faqReplyGroupIdDto.getMerId());
        if(faqReplyGroup == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组不存在");
        }

        BeanUtils.copyProperties(faqReplyGroup, faqReplyGroupVo);

        return faqReplyGroupVo;

    }

    /**
     * 删除智能回复分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param faqReplyGroupIdDto
     * @return null
     */
    public void deleteFaqReplyGroup(FaqReplyGroupIdDto faqReplyGroupIdDto){
        if(faqReplyGroupIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择分组");
        }

        //查询该分组是否已经被使用
        FaqReplyQuery userTypeQuery = new FaqReplyQuery();
        userTypeQuery.setMerId(faqReplyGroupIdDto.getMerId());
        userTypeQuery.setGroupId(faqReplyGroupIdDto.getId());
        List<FaqReply> userTypeList = faqReplyMapper.findByList(userTypeQuery);
        if(userTypeList != null && userTypeList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组已经被使用，不能删除");
        }
        faqReplyGroupMapper.deleteById(faqReplyGroupIdDto.getId(), faqReplyGroupIdDto.getMerId());
    }

    /**
     * 修改智能回复分组分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param faqReplyGroupDto
     * @return null
     */
    public void updateFaqReplyGroup(FaqReplyGroupDto faqReplyGroupDto){

        if(faqReplyGroupDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(StringUtils.isBlank(faqReplyGroupDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        FaqReplyGroup faqReplyGroup = faqReplyGroupMapper.selectById(faqReplyGroupDto.getId(), faqReplyGroupDto.getMerId());
        if(faqReplyGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该类型分组不存在");
        }

        //验证新名字是否已经存在
        if(!faqReplyGroupDto.getName().equals(faqReplyGroup.getName())){ //表示修改了组名称
            //验证新名称是否存在
            FaqReplyGroup faqReplyGroup_name = faqReplyGroupMapper.selectByName(faqReplyGroupDto.getMerId(), faqReplyGroupDto.getName());
            if(faqReplyGroup_name != null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
            }
        }

        Integer now = DateUtil.getNowTime();
        faqReplyGroup.setName(faqReplyGroupDto.getName());
        faqReplyGroup.setUpdateTime(now);
        faqReplyGroupMapper.update(faqReplyGroup);
    }

    /**
     * 查询所有智能回复分组列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param faqReplyGroupQuery
     * @return null
     */
    public List<FaqReplyGroupVo> findFaqReplyGroupByList(FaqReplyGroupQuery faqReplyGroupQuery){
        //新建返回到页面的LIST对象
        List<FaqReplyGroupVo> faqReplyGroupVoList = new ArrayList<>();
        List<FaqReplyGroup> faqReplyGroupList = faqReplyGroupMapper.findByList(faqReplyGroupQuery);

        if(faqReplyGroupList != null && faqReplyGroupList.size() > 0){
            for (FaqReplyGroup faqReplyGroup : faqReplyGroupList){
                FaqReplyGroupVo FaqReplyGroupVo = new FaqReplyGroupVo();
                BeanUtils.copyProperties(faqReplyGroup, FaqReplyGroupVo);
                faqReplyGroupVoList.add(FaqReplyGroupVo);
            }
        }
        //将分页信息复制到返回到页面的分页MO当中
        return faqReplyGroupVoList;
    }

    /**
     * 批量编辑智能回复分组
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param batchUpdateFaqReplyGroupDto
     * @return null
     */
    public void batchUpateFaqReplyGroup(BatchUpdateFaqReplyGroupDto batchUpdateFaqReplyGroupDto){

        List<Long> idList =  batchUpdateFaqReplyGroupDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择回复语");
        }

        Long groupId = batchUpdateFaqReplyGroupDto.getGroupId();
        if(groupId == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择分组");
        }
        Long merId = batchUpdateFaqReplyGroupDto.getMerId();
        int now = DateUtil.getNowTime();
        for (Long id : idList){
            FaqReply faqReply = faqReplyMapper.selectById(id, batchUpdateFaqReplyGroupDto.getMerId());
            if(faqReply != null){
                faqReply.setGroupId(groupId);
                faqReply.setUpdateTime(now);
                faqReplyMapper.updateById(faqReply);
            }
        }
    }

}
